import type { App } from 'vue';
import { 
  getDictList, 
  getDictValueByName, 
  getDictNameByValue, 
  clearDictList,
  getDictListSync,
  getDictNameByValueSync,
  getDictValueByNameSync,
  type DictItem 
} from '@/utils/dict';

// 声明全局属性类型
declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $getDictList: (typeCode: string) => DictItem[];
    $getDictListAsync: (typeCode: string) => Promise<DictItem[]>;
    $getDictValueByName: (typeCode: string, dictName: string) => string | null;
    $getDictValueByNameAsync: (typeCode: string, dictName: string) => Promise<string | null>;
    $getDictNameByValue: (typeCode: string, dictValue: string) => string | null;
    $getDictNameByValueAsync: (typeCode: string, dictValue: string) => Promise<string | null>;
    $clearDictList: () => void;
  }
}

export default {
  install(app: App) {
    // 同步方法（从缓存获取，用于模板）
    app.config.globalProperties.$getDictList = getDictListSync;
    app.config.globalProperties.$getDictValueByName = getDictValueByNameSync;
    app.config.globalProperties.$getDictNameByValue = getDictNameByValueSync;
    
    // 异步方法（用于脚本中预加载）
    app.config.globalProperties.$getDictListAsync = getDictList;
    app.config.globalProperties.$getDictValueByNameAsync = getDictValueByName;
    app.config.globalProperties.$getDictNameByValueAsync = getDictNameByValue;
    
    // 清空字典缓存
    app.config.globalProperties.$clearDictList = clearDictList;
  }
}; 